/*
 *  Copyright 2007 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core;

import com.blandware.atleap.model.core.Localizable;
import com.blandware.atleap.webapp.util.core.WebappUtil;
import org.apache.struts.taglib.TagUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

/**
 * <p>Tag that shows info about localizable: its type and title.
 * </p>
 * <p>List of allowed attributes:
 * <ul>
 * <li>
 * <b>localizable</b> - localizable about which info should be shown
 * </li>
 * <li>
 * <b>localizableId</b> - ID of localizable about which info should be shown
 * </li>
 * </ul>
 * </p>
 * <p>At least one of <b>localizable</b> and <b>localizableId</b> must be
 * specified. <b>localizable</b> has precedence over <b>localizableId</b>.
 * </p>
 * <p><a href="LocalizableInfoTag.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.1 $ $Date: 2007/12/17 15:15:27 $
 * @jsp.tag name="localizableInfo"
 * body-content="empty"
 */
public class LocalizableInfoTag extends SimpleTagSupport {

    /**
     * Localizable about which info should be shown
     */
    protected Localizable localizable;
    /**
     * ID of localizable about which info should be shown
     */
    protected Long localizableId;

    /**
     * Returns localizable about which info should be shown.
     *
     * @return localizable
     * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="com.blandware.atleap.model.core.Localizable"
	 * description="Localizable which info should be shown."
     */
    public Localizable getLocalizable() {
        return localizable;
    }

    /**
     * Sets localizable about which info should be shown.
     *
     * @param localizable   localizable to set
     */
    public void setLocalizable(Localizable localizable) {
        this.localizable = localizable;
    }

    /**
     * Returns ID of localizable about which info should be shown.
     *
     * @return ID of localizable
     * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.Long"
	 * description="ID of localizable which info should be shown."
     */
    public Long getLocalizableId() {
        return localizableId;
    }

    /**
     * Sets ID of localizable about which info should be shown.
     *
     * @param localizableId     ID to set
     */
    public void setLocalizableId(Long localizableId) {
        this.localizableId = localizableId;
    }

    /**
     * Does main job.
     *
     * @throws JspException
     * @throws IOException
     */
    public void doTag() throws JspException, IOException {
        PageContext pageContext = (PageContext) getJspContext();
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        String result;
        if (localizable != null) {
            result = WebappUtil.getLocalizableInfo(localizable, request);
        } else if (localizableId != null) {
            result = WebappUtil.getLocalizableInfo(localizableId, request);
        } else {
            throw new JspException("Neither localizable nor localizableId is specified");
        }
        TagUtils tagUtils = TagUtils.getInstance();
        tagUtils.write(pageContext, result);
    }
}
